package com.stedu.mall.userservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.bean.User;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.userservice.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(String username, String password) throws SteduException {
     //根据用户名查询判断用户是否存在
        User user = userMapper.selectByUsername(username);
        if(user.getStatus()==2){
            throw new SteduException("账号已被禁用");
        }
        if(user.getUsername()==null){
            throw new SteduException("用户不存在，无法登录");
        }
        //判断用户是否注销
           if(user.getStatus().equals(3)){
               throw new SteduException("用户已经注销，无法登录");
           }
        //对输入密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password+user.getSalt()));
        //和数据库中的密码进行对比
        if(!md5Pwd.equals(user.getPassword())){
            throw new SteduException("密码输入错误，无法登录");
        }
        return user;
    }

    @Override
    public boolean insert(User user) throws SteduException {
       //判断该用户名在系统中是否存在

        if(userMapper.selectByUsername(user.getUsername()) !=null){
            throw new SteduException("用户已存在，无法注册");
        }
        //生成盐
        String  salt= RandomUtil.randomString(8);
        user.setSalt(salt);
        //对密码进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(user.getPassword()+salt));
        user.setPassword(md5Pwd);
      //添加注册时间
        Date Time =new Date();
        user.setRegTime(Time);
        //设置状态
        user.setStatus(0);
        //添加用户
        return userMapper.insert(user)==1;
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public boolean updateInfo(User u) throws SteduException {
        User olduser = userMapper.selectById(u.getId());
            //用户名不允许修改
        if(!u.getUsername().equals(olduser.getUsername()))
        {
            throw  new SteduException("用户名不允许修改");
        }
        u.setRealname(null);
        u.setIdCard(null);
        u.setMoney(null);
        return userMapper.update(u)==1;
    }

    @Override
    public boolean update(User u) throws SteduException {
        //支付时修改金额
        return userMapper.update(u)==1;
    }

    @Override
    public boolean updateMoney(User u) throws SteduException {
             User olduser = userMapper.selectById(u.getId());
             //是充值(只需要支付密码)
             if(u.getPayPassword() == null)
             {
                 throw  new SteduException("支付密码为空，请前去设置支付密码在进行充值");
             }
             String md5Pwd = SecureUtil.md5(SecureUtil.md5(u.getPayPassword()+olduser.getSalt()));
             String oldPwd = olduser.getPayPassword();
             if(!(md5Pwd.equals(oldPwd))){

                 throw  new SteduException("支付密码不对，请确认支付密码后在进行充值");
             }
             u.setPayPassword(null);
             u.setMoney(u.getMoney().add(olduser.getMoney()));
             return userMapper.update(u)==1;
    }

    @Override
    public boolean updatePayPwd(Integer id, String newPayPwd, String oldPayPwd) throws SteduException {
        if (id ==null){
            throw new SteduException("您还未登录无法修改");
        }
        User olduser = userMapper.selectById(id);
        //进行加盐加密
        String oldMd5Pwd = SecureUtil.md5(SecureUtil.md5(oldPayPwd+olduser.getSalt()));
        if(!olduser.getPayPassword().equals(oldMd5Pwd)){

            throw new SteduException("原密码错误,无法修改");
        }
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(newPayPwd+olduser.getSalt()));
        //设置新密码
        olduser.setPayPassword(md5Pwd);
        return userMapper.update(olduser)==1;
    }

    @Override
    public boolean updatePwd(Integer id, String newPayPwd, String oldPayPwd) throws SteduException {
        if (id ==null){
            throw new SteduException("您还未登录无法修改");
        }
        User olduser = userMapper.selectById(id);
        //进行加盐加密
        String oldMd5Pwd = SecureUtil.md5(SecureUtil.md5(oldPayPwd+olduser.getSalt()));
        if(!olduser.getPassword().equals(oldMd5Pwd)){

            throw new SteduException("原登录密码错误,无法修改");
        }
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(newPayPwd+olduser.getSalt()));
        //设置新密码
        olduser.setPassword(md5Pwd);
        return userMapper.update(olduser)==1;
    }

    @Override
    public boolean updateCertifying(User u) throws SteduException {
        User olduser = userMapper.selectById(u.getId());
        if(!u.getRealname().equals(olduser.getRealname())){

            throw new SteduException("认证失败");
        }
        if(!u.getIdCard().equals(olduser.getIdCard())){
            throw new SteduException("认证失败");
        }
        olduser.setStatus(1);
        return userMapper.update(olduser)==1;
    }

    @Override
    public boolean updateSetPwd(Integer id, String loginPwd, String payPwd) throws SteduException {
        if (id ==null){
            throw new SteduException("您还未登录无法修改");
        }
        User olduser = userMapper.selectById(id);
        //进行加盐加密
        String oldMd5Pwd = SecureUtil.md5(SecureUtil.md5(loginPwd+olduser.getSalt()));
        if(!olduser.getPassword().equals(oldMd5Pwd)){

            throw new SteduException("登录密码错误,无法设置");
        }
        if(olduser.getPayPassword()!=null){

            throw new SteduException("支付密码已经设置,无需重复设置");
        }

        String md5Pwd = SecureUtil.md5(SecureUtil.md5(payPwd+olduser.getSalt()));
        //设置新密码
        olduser.setPayPassword(md5Pwd);
        return userMapper.update(olduser)==1;
    }

    @Override
    public PageInfo<User> condition(User condition, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList=userMapper.selectByCondition(condition);
        PageInfo<User> pageInfo=new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public boolean resetPwd(User user) {
        String pwd= user.getPassword();
        String salt = user.getSalt();
        String md5Pwd= SecureUtil.md5(SecureUtil.md5(pwd+user.getSalt()));
        user.setPassword(md5Pwd);
        return userMapper.update(user)==1;
    }
}
